<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/26
 * Time: 10:50
 */

namespace Api\Controller;


use Common\Api\AccountApi;
use Common\ConstVar\BoyeActionConstModel;
use Uclient\Model\OAuth2TypeModel;
use Uclient\Model\UcenterMemberModel;

class QQController extends ApiController {
    private $appid;
    private $appsecret;

    public function _init(){
        parent::_init();
        //QQ开放应用的appid和appsecret
        $this->appid = "1104887406";
        $this->appsecret = "7mxqFi07TN8QD1ZR";
        vendor('OpenApiV3',APP_PATH.'Api/Vendor/QQSDK3.0/','.php');
    }

    /**
     * 调用QQ登录接口
     */
    public function login(){

        // 用户的OpenID/OpenKey
        $openid = $this->_post('openid','','缺少openid');// 'A753A61B03F76F9BAC3B92A8B837C6BF';
        $openkey =  $this->_post('openkey','','缺少openkey');//'9C20E094DC3D81278AD0525576B6DC3C';

        //1. 检测是否注册过
        $result = apiCall(AccountApi::GET_INFO_BY_QQ_OPENID,array($openid));

        addLog("QQ/login",$result,$result,"QQ登录从数据库获取的用户信息".$openid);
        //如果已经存在，则返回信息
        if($result['status'] && is_array($result['info'])){
            $this->apiReturnSuc($result['info']);
        }elseif($result['status'] === false){
            $this->apiReturnErr($result['info']);
        }


        // OpenAPI的服务器IP
        // 最新的API服务器地址请参考wiki文档: http://wiki.open.qq.com/wiki/API3.0%E6%96%87%E6%A1%A3
        $server_name = 'openapi.tencentyun.com';
//        $server_name = '119.147.19.43';


        // 所要访问的平台, pf的其他取值参考wiki文档: http://wiki.open.qq.com/wiki/API3.0%E6%96%87%E6%A1%A3
        $pf = 'openmobile_ios';

        $sdk = new \OpenApiV3($this->appid, $this->appsecret);
        $sdk->setServerName($server_name);

        $result = $this->get_user_info($sdk, $openid, $openkey, $pf);

        addLog("QQ/login",$result,$result,"从腾讯拉取的QQ用户信息".$openid);

        if($result['ret'] == 0){

            $password = md5($openid);
            $nickname = $result['nickname'];
            $head = $result['figureurl'];
            $idcode = getIDCode(rand(10000000000,99999999999),'Q');
//            date('YmdHis',time());
            $entity = array(
                'type'=> UcenterMemberModel::ACCOUNT_TYPE_USERNAME,
                'username' => $openid,
                'password' => $password,
                'from' => OAuth2TypeModel::QQ,
                'mobile' => '',
                'realname' => '',
                'nickname' => $nickname ,
                'email' => '',
                'idnumber' => '',
                'birthday' => time(),
                'idcode'=>$idcode,
                'head'=>$head,
                'qq_openid'=>$openid,
            );

            $result = apiCall(AccountApi::REGISTER, array($entity));

            addLog("QQ/login",$result,$result,"QQ登录-注册");
            if($result['status']){
                $uid = $result['info'];
                if($uid > 0){
                    $result = apiCall(AccountApi::GET_INFO,array($uid));

                    if($result['status']){
                        action_log(BoyeActionConstModel::API_USER_LOGIN, "member", $uid, $uid);
                        $this->apiReturnSuc($result['info']);
                    }
                }
            }

            $this->apiReturnErr($result['info']);


        }else if($result['ret'] >= -50 && $result['ret'] <= -1){
            $this->apiReturnErr("接口调用不能通过接口代理机校验，需要开发者进行相应的处理。");
        }else if($result['ret'] < -50){
            $this->apiReturnErr("系统内部错误，请通过企业QQ联系技术支持，调查问题原因并获得解决方案。");
        }elseif($result['ret'] > 0){
            $this->apiReturnErr("调用QQ API时发生错误。");
        }

        $this->apiReturnErr("未知原因，导致登录失败!");

    }

    /**
     * 获取好友资料
     *
     * @param object $sdk OpenApiV3 Object
     * @param string $openid openid
     * @param string $openkey openkey
     * @param string $pf 平台
     * @return array 好友资料数组
     */
    function get_user_info($sdk, $openid, $openkey, $pf)
    {
        $params = array(
            'openid' => $openid,
            'openkey' => $openkey,
            'pf' => $pf,
        );

        $script_name = '/v3/user/get_info';
        return $sdk->api($script_name, $params,'post');
    }

}